//
//  TestViewController.m
//  UIViewLifeCycleTest
//
//  Created by anchor on 2017/7/14.
//  Copyright © 2017年 QDHome. All rights reserved.
//

#import "TestViewController.h"
#import "TestView.h"

@interface TestViewController ()

@property (strong, nonatomic) TestView       *containerView;

@end

@implementation TestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor lightGrayColor];
    
   self.containerView = [[TestView alloc] init];
    self.containerView.frame = CGRectMake(8, 100, self.view.bounds.size.width - 8*2, 250);
    self.containerView.backgroundColor = [UIColor colorWithRed:0.101 green:0.502 blue:0.427 alpha:1.000];
    [self.view addSubview:self.containerView];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 400, 100, 40);
    btn.backgroundColor = [UIColor colorWithRed:0.489 green:0.548 blue:0.898 alpha:1.000];
    [btn setTitle:@"add/remove" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(addView:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame = CGRectMake(110, 400, 100, 40);
    btn2.backgroundColor = [UIColor colorWithRed:0.489 green:0.548 blue:0.898 alpha:1.000];
    [btn2 setTitle:@"superchange" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(changeSuperSize) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn2];
    
    UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn3.frame = CGRectMake(220, 400, 100, 40);
    btn3.backgroundColor = [UIColor colorWithRed:0.489 green:0.548 blue:0.898 alpha:1.000];
    [btn3 setTitle:@"subchange" forState:UIControlStateNormal];
    [btn3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn3 addTarget:self action:@selector(changeSubSize) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn3];
}

- (void)addView:(UIButton *)sender{
    UIView *sub =  [self.containerView viewWithTag:99];
    if (sub) {
        [sub removeFromSuperview];
    } else {
        sub = [[TestView alloc] init];
        sub.frame = CGRectMake(0, 0, 40, 40);
        sub.tag = 99;
        [self.containerView addSubview:sub];
    }
}

- (void)changeSuperSize
{
    if (self.containerView.frame.size.width != self.view.frame.size.width) {
        self.containerView.frame = CGRectMake(0, 0, self.view.frame.size.width, 250);
    } else {
          self.containerView.frame = CGRectMake(8, 100, self.view.bounds.size.width - 8*2, 250);
    }
}

- (void)changeSubSize
{
    UIView *sub =  [self.containerView viewWithTag:99];
    if (sub) {
        sub.frame = CGRectMake(0, 0, 100, 100);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
